<?php

namespace App\Http\Controllers;

use App\Models\CapitalLog;
use App\Models\Member;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class BaseController extends Controller
{
    //
    protected $official_account = null;
    protected $mini_app = null;
    protected $payment = null;
    protected $http_url = 'https://jhwz.dfgoikw.com';

    public function __construct()
    {
        $config = config('wechat.mini_program.default');
        $this->mini_app = Factory::miniProgram($config);

        $config_payment = config('wechat.payment.default');
        $this->payment = Factory::payment($config_payment);

        $config_official = config('wechat.official_account.default');
        $this->official_account = Factory::officialAccount($config_official);
    }

    /**
     * 生成3rd_session
     * @param int $len 长度
     * @return string
     */
    protected function _3rd_session($len=64)
    {
        $fp = @fopen('/dev/urandom', 'rb');
        $result = '';
        if ($fp !== FALSE) {
            $result .= @fread($fp, $len);
            @fclose($fp);
        } else {
            trigger_error('不能打开 /dev/urandom.');
        }
        // convert from binary to string
        $result = base64_encode($result);
        // remove none url chars
        $result = strtr($result, '+/', '-_');

        return substr($result, 0, $len);
    }

    /**
     * 返回成功方法
     * @param $msg 消息
     * @param $data 数据
     */
    public function success($msg='', $data=[])
    {
        return response()->json([
            'code' => 1,
            'msg' => $msg,
            'data' => $data
        ]);
    }

    /**
     * 返回失败方法
     * @param $msg
     * @param $code
     */
    public function error($msg='', $code=0)
    {
        return response()->json([
            'code' => $code,
            'msg' => $msg
        ]);
    }

    //获取验证信息通过3rd
    public function getSessionBy3rd($_3rd_session)
    {
        $_3rd_session = unserialize(Redis::get($_3rd_session));

        return $_3rd_session;
    }

    //获取用户信息通过3rd
    public function getMemberBy3rd($_3rd_session)
    {
        $_3rd_session = unserialize(Redis::get($_3rd_session));
        try{
            $result = Member::where('openid', $_3rd_session['openid'])->first()->toArray();
        }catch (\Exception $e){
            return $this->error('用户不存在');
        }
        return $result;
    }

    //今日收益
    public function getTodaySy($member_id)
    {
        $today = date('Y-m-d');
        $info = CapitalLog::selectRaw('sum(money) as money, sum(money_cash) as money_cash')
            ->where('member_id',$member_id)
            ->where('date',$today)
            ->groupby('member_id')
            ->get();
        return $info;
    }

    //西瓜视频地址转换
    protected function ixiguaHandle($videoid)
    {
        $s = getUrl($videoid);
        $json = $this->requestUrl($s);
        $arr = json_decode($json, 1);
        try{
            $main_url = $arr['data']['video_list']['video_1']['main_url'];
            $main_url = base64_decode($main_url);
        }catch (\Exception $e) {
            $main_url = '';
        }

        return $main_url;
    }

    protected function requestUrl($URL)
    {
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($c, CURLOPT_HEADER, 1);//输出远程服务器的header信息
        curl_setopt($c, CURLOPT_USERAGENT, 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat');
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        curl_close($c);
        if ($contents) {
            return $contents;
        } else {
            return FALSE;
        }
    }

}
